package com.alfayun.fiscalriskanalysis.analysis;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.alfayun.fiscalriskanalysis.analysisspec.AnalysisSpec;
import com.alfayun.fiscalriskanalysis.indicator.Indicator;
import com.alfayun.fiscalriskanalysis.report.Report;

@Entity
@Table(name = "analysis")
public final class Analysis {

	@Column(name = "analysis_id", updatable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	private Integer id;
	public String name;
	public Integer enterpriseId;
	public LocalDate startDate;
	public LocalDate endDate;
	
	@OneToOne
	@JoinColumn(name = "analysis_spec_id")
	private AnalysisSpec spec;
	@Transient
	private List<Indicator> indicators;
	@Transient
	private Collection<Report> reports;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public AnalysisSpec getSpec() {
		return spec;
	}

	public void setSpec(AnalysisSpec spec) {
		this.spec = spec;
	}

	public List<Indicator> getIndicators() {
		return indicators;
	}

	public void setIndicators(List<Indicator> indicators) {
		this.indicators = indicators;
	}

	public Collection<Report> getReports() {
		return reports;
	}

	public void setReports(Collection<Report> reports) {
		this.reports = reports;
	}

	public static Analysis analyse(AnalysisSpec spec, Collection<Report> reports, LocalDate startDate,
			LocalDate endDate) {
		return new Analysis(spec, reports, startDate, endDate);
	}

	private Analysis(AnalysisSpec spec, Collection<Report> reports, LocalDate startDate, LocalDate endDate) {
		this.spec = spec;
		this.startDate = startDate;
		this.endDate = endDate;
		this.reports = reports;

		indicators = spec.getAllIndicator().stream().map(e -> new Indicator(e, reports)).collect(Collectors.toList());
	}

}
